/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.servlet;

import br.com.gestaoFuneraria.DAO.AssociadoDAO;
import br.com.gestaoFuneraria.modelo.Associado;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class cadastro extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
           // Integer codigo = Integer.parseInt(request.getParameter("codigo"));
            String nome = request.getParameter("nome");
            String rg = request.getParameter("rg");
            String cpf = request.getParameter("cpf");
            String titulo = request.getParameter("titulo");
            String sexo = request.getParameter("sexo");
            String filiacao = request.getParameter("filiacao");
            String data = request.getParameter("data");
            String endereco = request.getParameter("endereco");
            String cidade = request.getParameter("cidade");
            String estado = request.getParameter("estado");
            String cep = request.getParameter("cep");
            Integer cdfunc = Integer.parseInt(request.getParameter("cdfunc"));
            Associado ass = new Associado(nome, rg, estado, cpf, titulo, sexo, filiacao, data, endereco, cidade, cep, cdfunc);
            AssociadoDAO bolota = new AssociadoDAO();
            bolota.cadastrar(ass);
            out.println("Cadastro efetuado com Sucesso!!");
            

        } finally {
            response.sendRedirect("http://localhost:8080/gesFun/associado.jsp");
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
            
        } catch (SQLException ex) {
            Logger.getLogger(cadastro.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            response.sendRedirect("http://localhost:8080/gesFun/associado.jsp");
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(cadastro.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
